<meta charset="utf-8">
(#) `onClick` method does not exist

!!! ERROR: `onClick` method does not exist
   This is an error.

Id
:   `OnClick`
Summary
:   `onClick` method does not exist
Severity
:   Error
Category
:   Correctness
Platform
:   Android
Vendor
:   Android Open Source Project
Feedback
:   https://issuetracker.google.com/issues/new?component=192708
Since
:   Initial
Affects
:   Kotlin and Java files and resource files
Editing
:   This check runs on the fly in the IDE editor
Implementation
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/OnClickDetector.java)
Tests
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/OnClickDetectorTest.java)
Copyright Year
:   2012

The `onClick` attribute value should be the name of a method in this
View's context to invoke when the view is clicked. This name must
correspond to a public method that takes exactly one parameter of type
`View`.

Must be a string value, using '\\;' to escape characters such as '\\n'
or '\\uxxxx' for a unicode character.

(##) Example

Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
res/layout/onclick.xml:10:Error: Corresponding method handler 'public
void nonexistent(android.view.View)' not found [OnClick]
    android:onClick="nonexistent"
    -----------------------------
res/layout/onclick.xml:16:Error: Corresponding method handler 'public
void wrong1(android.view.View)' not found [OnClick]
    android:onClick="wrong1"
    ------------------------
res/layout/onclick.xml:22:Error: Corresponding method handler 'public
void wrong2(android.view.View)' not found [OnClick]
    android:onClick="wrong2"
    ------------------------
res/layout/onclick.xml:28:Error: Corresponding method handler 'public
void wrong3(android.view.View)' not found [OnClick]
    android:onClick="wrong3"
    ------------------------
res/layout/onclick.xml:58:Error: Corresponding method handler 'public
void simple_typo(android.view.View)' not found (did you mean
OnClickActivity#simple_tyop ?) [OnClick]
    android:onClick="simple_typo"
    -----------------------------
res/layout/onclick.xml:82:Error: onClick handler method name cannot
start with the character '1' [OnClick]
    android:onClick="1invalidname"
                     ------------
res/layout/onclick.xml:88:Error: There should be no spaces in the
onClick handler name [OnClick]
    android:onClick="invalid name"
                     ------------
res/layout/onclick.xml:94:Error: onClick handler method name cannot
contain the character '(' [OnClick]
    android:onClick="invalidname()"
                     -------------
res/layout/onclick.xml:100:Error: onClick handler method name cannot be
a Java keyword [OnClick]
    android:onClick="new"
                     ---
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here are the relevant source files:

`res/layout/onclick.xml`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~xml linenumbers
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" &gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="nonexistent"
        android:text="Button" /&gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="wrong1"
        android:text="Button" /&gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="wrong2"
        android:text="Button" /&gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="wrong3"
        android:text="Button" /&gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="ok2"
        android:text="Button" /&gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="wrong5"
        android:text="Button" /&gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="wrong6"
        android:text="Button" /&gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="ok"
        android:text="Button" /&gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="simple_typo"
        android:text="Button" /&gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="my\u1234method"
        android:text="Button" /&gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="wrong7"
        android:text="Button" /&gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="@string/ok"
        android:text="Button" /&gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="1invalidname"
        android:text="Button" /&gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="invalid name"
        android:text="Button" /&gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="invalidname()"
        android:text="Button" /&gt;

    &lt;Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="new"
        android:text="Button" /&gt;

&lt;/LinearLayout&gt;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`src/test/pkg/OnClickActivity.java`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers
package test.pkg;

import android.app.Activity;
import android.util.Log;
import android.view.View;

/** Test data for the OnClickDetector */
public class OnClickActivity extends Activity {
    // Wrong argument type 1
    public void wrong1() {
    }

    // Wrong argument type 2
    public void wrong2(int i) {
    }

    // Wrong argument type 3
    public void wrong3(View view, int i) {
    }

    // Return type is allowed to not be void
    public int ok2(View view) {
        return 0;
    }

    // Wrong modifier (not public)
    void wrong5(View view) {
    }

    // Wrong modifier (is static)
    public static void wrong6(View view) {
    }

    public void ok(View view) {
    }

    // Ok: Unicode escapes
    public void myሴmethod(View view) {
    }

    // Typo
    public void simple_tyop(View view) {
    }

    void wrong7(View view) {
        Log.i("x", "wrong7: called");
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/OnClickDetectorTest.java)
for the unit tests for this check to see additional scenarios.

The above example was automatically extracted from the first unit test
found for this lint check, `OnClickDetector.test`.
To report a problem with this extracted sample, visit
https://issuetracker.google.com/issues/new?component=192708.

(##) Suppressing

You can suppress false positives using one of the following mechanisms:

* Adding the suppression attribute `tools:ignore="OnClick"` on the
  problematic XML element (or one of its enclosing elements). You may
  also need to add the following namespace declaration on the root
  element in the XML file if it's not already there:
  `xmlns:tools="http://schemas.android.com/tools"`.

* Using a suppression annotation like this on the enclosing
  element:

  ```kt
  // Kotlin
  @Suppress("OnClick")
  fun method() {
     problematicStatement()
  }
  ```

  or

  ```java
  // Java
  @SuppressWarnings("OnClick")
  void method() {
     problematicStatement();
  }
  ```

* Using a suppression comment like this on the line above:

  ```kt
  //noinspection OnClick
  problematicStatement()
  ```

* Using a special `lint.xml` file in the source tree which turns off
  the check in that folder and any sub folder. A simple file might look
  like this:
  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;lint&gt;
      &lt;issue id="OnClick" severity="ignore" /&gt;
  &lt;/lint&gt;
  ```
  Instead of `ignore` you can also change the severity here, for
  example from `error` to `warning`. You can find additional
  documentation on how to filter issues by path, regular expression and
  so on
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).

* In Gradle projects, using the DSL syntax to configure lint. For
  example, you can use something like
  ```gradle
  lintOptions {
      disable 'OnClick'
  }
  ```
  In Android projects this should be nested inside an `android { }`
  block.

* For manual invocations of `lint`, using the `--ignore` flag:
  ```
  $ lint --ignore OnClick ...`
  ```

* Last, but not least, using baselines, as discussed
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).

<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://morgan3d.github.io/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>